* --------------------------------------------------
* DIRS
* --------------------------------------------------
cd 
global inPublic ""
global inConf ""
global out "L:\LBD_initiative\BDS-IF\BDS-HG\ReStat_Replication\out"
global fig "$out/figures/"
global tbl "$out/tables/"

* --------------------------------------------------
* PARAMS 
* --------------------------------------------------
set seed 20191126
local lhsVars = "dhs_emp dhs_rev ln_emp ln_rev ln_prod"

*--------------------------------------------------
* GRAPHING PARAMETERS
*--------------------------------------------------
graph set window fontface "Times New Roman"
local  inv_golden_ratio = 2/(sqrt(5)+1)
local aspratio = .57142857 
local ysz = 7
local xsz = 13

local c1="27 158 119"
local c2="217 95 2"
local c3="117 112 179"
local c51="228 26 28"
local c52="55 126 184"
local c53="77 175 74"
local c54="152 78 163"
local c55="255 127 0"

* -------------------------------------------------
* ADOs
*--------------------------------------------------
capture saveRegs drop saveRegs
program define saveRegs
    args treatedonly er2 en file ogrp o
    use $out/hold.dta, clear
    if `treatedonly' == 1 {
        keep if regexm(parm, "treated")
    }
    gen double r2 = `er2' if _n==1
    gen double n = `en' if _n==1
    rename r2 rsq
    keep parm estimate stderr rsq n
    export excel using $out/`file'.xlsx, sheet(o`ogrp'.`o') firstrow(variables) sheetmodify keepcellfmt
end

capture recodeFAge drop recodeFAge
program define recodeFAge
    args in out
    gen `out' = "b) 1" if `in'==1
    replace `out' = "c) 2" if `in'==2
    replace `out' = "d) 3" if `in'==3
    replace `out' = "e) 4" if `in'==4
    replace `out' = "f) 5" if `in'==5
    replace `out' = "g) 6 to 10" if `in'>=6 & `in'<11
    replace `out' = "h) 11 to 20" if `in'>=11 & `in'<21
    replace `out' = "i) 21+" if `in'>=21 & !missing(`in')
end

capture captureMatrix drop captureMatrix 
program define captureMatrix 
    args varName
    svmat double A, names(cols)
    gen lbl=0
    reshape long cols, i(lbl) j(v) 
    gen var = ""
    replace var = "Firm Age" if v==1
    replace var = "Employment" if v==2
    replace var = "log(Employment)" if v==3
    replace var = "log(Revenue)" if v==4
    replace var = "log(Labor Productivity)" if v==5
    replace var = "Avg Age of FT" if v==6
    rename cols `varName'
    keep var `varName'
end

capture makeRegTable drop makeRegTable 
program define makeRegTable 
	* prepares regression tex table from output in output2020202
	*rename parm _parm
	*gen double parm = _parm
	*drop _parm
    desc 
    local newObs = `r(N)' + 3
    set obs `newObs'
    replace parm = "r2" if _n==`newObs'-1
    replace estimate = rsq[1] if _n==`newObs'-1
    replace parm = "N" if _n==`newObs'
    replace estimate = n[1] if _n==`newObs'
    gen e = estimate
    tostring estimate, replace force
    replace estimate = "" if estimate == "."
    replace estimate = estimate + "***" if (abs(e/stderr)) > 3.291 & regexm(estimate,"\*") != 1 & parm != "r2" & parm != "N" & estimate != "" 
    replace estimate = estimate + "**" if (abs(e/stderr)) > 1.96 & regexm(estimate,"\*") != 1 & parm != "r2" & parm != "N" & estimate != "" 
    replace estimate = estimate + "*" if (abs(e/stderr)) > 1.645 & regexm(estimate,"\*") != 1 & parm != "r2" & parm != "N" & estimate != ""
    replace parm = subinstr(parm, "#"," \times ",.)
    replace parm = subinstr(parm, "kftp_hct_treated_post","Post \times Treated \times HCT \times Founder",.)
    replace parm = subinstr(parm, "hct_treated_post","Post \times Treated \times HCT",.)

    replace parm = subinstr(parm, "kftp_col_treated_post","Post \times Treated \times College \times Founder",.)
    replace parm = subinstr(parm, "col_treated_post","Post \times Treated \times College",.)

    replace parm = subinstr(parm, "kftp_sabst_treated_post","Post \times Treated \times Abst Task \times Founder",.)
    replace parm = subinstr(parm, "sabst_treated_post","Post \times Treated \times Abst Task",.)

    replace parm = subinstr(parm, "kftp_sprp_treated_post","Post \times Treated \times Sole Prop \times Founder",.)
    replace parm = subinstr(parm, "sprp_treated_post","Post \times Treated \times Sole Prop",.)

    replace parm = subinstr(parm, "1.post","Post",.)
    replace parm = subinstr(parm, "1.treated","Treated",.)
    replace parm = subinstr(parm, "1.kft_prime","Founder",.)
    replace parm = subinstr(parm, "1.kft","Founder",.)
    replace parm = subinstr(parm, "1.small","Small",.)
    replace parm = subinstr(parm, "1.sprp","Sole Prop",.)
    replace parm = subinstr(parm, "1.young","Yg Firm",.)
    replace parm = subinstr(parm, "1.hitech","HT",.)
    replace parm = subinstr(parm, "1.hp","HP",.)
    replace parm = subinstr(parm, "1.gtmed","Old FT",.)
    replace parm = subinstr(parm, "1.b2b","B2B",.)
    replace parm = subinstr(parm, "1.fran","Franchise",.)
    replace parm = subinstr(parm, "1.fageshock_2_5","Fage 2-5",.)
    replace parm = subinstr(parm, "xdev_earn_treated_post","Post \times Treated \times Prior Earn",.)
    *replace parm = "Post \times Treated" if parm == "1.post#1.treated"
    *replace parm = "Post" if parm == "1.post"
    tostring stderr, replace force
    replace stderr = "" if stderr == "."
    gen order = _n
    preserve
    drop if parm=="r2" | parm=="N" | parm==""
    keep order stderr
    replace stderr = "(" + stderr + ")"
    replace order = order + .5
    tempfile se
    save `se'
    restore 
    append using `se'
    sort order 
    replace estimate = stderr if substr(stderr, 1,1)=="("
    replace parm = "\$R^2\$" if parm=="r2"
    keep parm order estimate
end

capture getDHSMainReg drop getDHSMainReg
program define getDHSMainReg
	args sheet
	import excel using $out/dhs_emp.xlsx, sheet(o1.`sheet') clear first
	makeRegTable
	rename estimate dhsemp
	tempfile p1
	save `p1'
	import excel using $out/dhs_rev.xlsx, sheet(o2.`sheet') clear first
	makeRegTable
	rename estimate dhsrev
	merge 1:1 order using `p1', nogen
	keep parm dhsemp dhsrev
	order parm dhsemp dhsrev 
end

capture procCOPTable drop procCOPTable 
program define procCOPTable 
	* format conditional-on-positive regression output from output202002
    set obs 11
    replace parm = "treated" if _n==6
    replace parm = "control" if _n==7
    replace parm = "surv5" if _n==8
    replace parm = "NAICS4 FE" if _n==9
    replace parm = "Birth Yr FE" if _n==10
    replace parm = "Firm Age FE" if _n==11
    tostring estimate, replace 
    replace estimate = "`1'" if _n==6
    replace estimate = "`2'" if _n==7
    replace estimate = "`3'" if _n==8
    replace estimate = "`4'" if _n==9
    replace estimate = "`5'" if _n==10
    replace estimate = "`6'" if _n==11
    replace order = 2.1 if _n==6
    replace order = 2.2 if _n==7
    replace order = 2.3 if _n==8
    replace order = 2.4 if _n==9
    replace order = 2.5 if _n==10
    replace order = 2.6 if _n==11
end


capture procEJvFDRReg drop procEJvFDRReg 
program define procEJvFDRReg
    foreach num of numlist 0/15 {
        replace parm = "`num'" if parm == "`num'.year_to_death#1.treated"
    }
    destring parm, replace force
    sort parm
    gen year_to_death = parm-5
    drop if missing(year_to_death)
    gen minnintyfive = estimate - 1.96*stderr
    gen maxnintyfive = estimate + 1.96*stderr
    set obs 16
    replace year_to_death =-1 if missing(year_to_death)
    foreach v of varlist estimate minnintyfive maxnintyfive {
        replace `v' = 0 if missing(`v')
    }
    sort year_to_death
    gen p5=minnintyfive*maxnintyfive >=0
end


capture plotFdrVEj drop plotFdrVEj 
program define plotFdrVEj
    args c1 c2 ltop lbottom ylab ytop ybottom yincrmt outFile
	quietly sum year_to_death
    #delimit;
        tw (scatteri `lbottom' 0 `ltop' 0, c(l) lcolor(black) lwidth(thin) msym(none) yline(0, lcolor(black) lwidth(thin)))
        (rcap fdr0_minnintyfive fdr0_maxnintyfive year_to_death_fdr0, lpattern(dash) lwidth(medthin) lcolor("`c1'"))
        (scatter fdr0_estimate year_to_death_fdr0 if year_to_death!=-1 & fdr0_p5==1, mcolor("`c1'") msize(2) msym(circle))
        (scatter fdr0_estimate year_to_death_fdr0 if year_to_death!=-1 & fdr0_p5==0, mcolor("`c1'") msize(2) msym(circle_hollow))
        (scatter fdr0_estimate year_to_death_fdr0 if year_to_death==-1, mcolor(black) msize(3) msym(plus))
        (line fdr0_estimate year_to_death_fdr0, lpattern(solid) lcolor("`c1'") lwidth(medium))
        
        (rcap fdr1_minnintyfive fdr1_maxnintyfive year_to_death_fdr1, lpattern(dash) lwidth(medthin) lcolor("`c2'"))
        (scatter fdr1_estimate year_to_death_fdr1 if year_to_death!=-1 & fdr1_p5==1, mcolor("`c2'") msize(2) msym(circle))
        (scatter fdr1_estimate year_to_death_fdr1 if year_to_death!=-1 & fdr1_p5==0, mcolor("`c2'") msize(2) msym(circle_hollow))
        (scatter fdr1_estimate year_to_death_fdr1 if year_to_death==-1, mcolor(black) msize(3) msym(plus))
        (line fdr1_estimate year_to_death_fdr1, lpattern(solid) lcolor("`c2'") lwidth(medium))
        ,
        graphregion(color(white) lwidth(large)) bgcolor(white) 
        title("")
        ytitle("`ylab'", size(medlarge))
        xtitle("Year Relative to Death", size(medlarge))
        ysize(`ysz') xsize(`xsz')
        xlab(-5(1)`r(max)', labsize(medlarge))
        ylabel(`ybottom'(`yincrmt')`ytop',angle(horizontal) labsize(medlarge))
        xscale(range(-5 5))
        yscale(range(-.2 .1))
        yscale(nofextend titlegap(*-30))
        xscale(nofextend titlegap(*5))
        legend(order(3 "Early Joiner" 8 "Founder") region(col(white)) cols(2) size(medlarge))
        ;
    #delimit cr
    graph export $fig/`outFile'.png, replace
end


capture plotSyj drop plotSyj 
program define plotSyj
    args c1 ltop lbottom ylab ytop ybottom yincrmt outFile

    #delimit;
        tw (scatteri `lbottom' 0 `ltop' 0, c(l) lcolor(black) lwidth(thin) msym(none) yline(0, lcolor(black) lwidth(thin)))
        (rcap minnintyfive maxnintyfive year_to_death, lpattern(dash) lwidth(medthin) lcolor("`c1'"))
        (scatter estimate year_to_death if year_to_death!=-1 & p5==1, mcolor("`c1'") msize(2) msym(circle))
        (scatter estimate year_to_death if year_to_death!=-1 & p5==0, mcolor("`c1'") msize(2) msym(circle_hollow))
        (scatter estimate year_to_death if year_to_death==-1, mcolor(black) msize(3) msym(plus))
        (line estimate year_to_death, lpattern(solid) lcolor("`c1'") lwidth(medium))
        
        ,
        graphregion(color(white) lwidth(large)) bgcolor(white)
        title("")
        ytitle("`ylab'", size(medlarge))
        xtitle("Year Relative to Death", size(medlarge))
        ysize(`ysz') xsize(`xsz')
        xlab(-5(1)5, labsize(medlarge))
        ylabel(`ybottom'(`yincrmt')`ytop',angle(horizontal) labsize(medlarge))
        xscale(range(-5 5))
        yscale(range(-.2 .1))
        yscale(nofextend titlegap(*-30))
        xscale(nofextend titlegap(*5))
        legend(off)
        ;
    #delimit cr
    graph export $fig/`outFile'.png, replace
end
